﻿using Microsoft.AspNetCore.Mvc;
using 天离安全_天擎.Tl_Class_Services;

namespace 天离安全_天擎.Controllers.Port_Controller
{
    /// <summary>
    /// 端口扫描控制器
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class PortScanController : Controller
    {
        /// <summary>
        /// 全端口单IP地址扫描，默认端口1-65535
        /// </summary>
        /// <returns></returns>
        [HttpPost("Scan")]
        public async Task<IActionResult> PortScan([FromBody] PortScanRequest request)
        {
            // 扫描线程
            Tl_Api.PortScan_Api.Max = request.Max;
            // 开始端口
            Tl_Api.PortScan_Api.Str_Port = request.StrPort;
            // 结束端口
            Tl_Api.PortScan_Api.End_Por = request.EndPort;
            // 扫描的IP地址
            Tl_Api.PortScan_Api.Ip = request.Ip;

            // 异步执行
            await Tl_SerVices.PortScan_SerVices.PortScan_All();

            // 初始化参数
            var suesscc = Tl_Api.PortScan_Api.Scan_Info;
            var error = Tl_Api.PortScan_Api.Scan_Error;

            // 创建两个列表来存储结果
            var intResults = new List<int>();
            var nonIntResults = new List<string>();

            if (suesscc.Length == 0 && error.Length == 0)
            {
                return Ok(new { status = "504" });
            }
            if (suesscc.Length > 0)
            {
                var ports = suesscc.ToString().Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var port in ports)
                {
                    var trimmedPort = port.Trim();

                    if (int.TryParse(trimmedPort, out int result))
                    {
                        intResults.Add(result);
                    }
                    else
                    {
                        nonIntResults.Add(trimmedPort);
                    }
                }
                return Ok(new { status = "200", info = intResults.Distinct(), datetime = DateTime.Now.ToString() });
            }
            else
            {
                foreach (var errors in error.ToString())
                {
                    nonIntResults.Add(errors.ToString());
                }
            }
            return Ok(new { status = "500", error = nonIntResults, datetime = DateTime.Now.ToString() });
        }
        /// <summary>
        /// 扫描默认端口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("Default/Scan")]
        public async Task<IActionResult> Default_PortScan([FromBody] PortScanDefault request)
        {
            // IP地址
            Tl_Api.PortScan_Api.Ip = request.Ip;
            // 默认的端口，这是一个数组
            Tl_Api.PortScan_Api.Port = request.Ports;
            // 默认的线程
            Tl_Api.PortScan_Api.Max = request.Max;
            // 异步执行
            await Tl_SerVices.PortScan_SerVices.PortScan_Default();

            // 初始化参数
            var suesscc = Tl_Api.PortScan_Api.Scan_Info;
            var error = Tl_Api.PortScan_Api.Scan_Error;

            // 创建两个列表来存储结果
            var intResults = new List<int>();
            var nonIntResults = new List<string>();

            if (suesscc.Length == 0 && error.Length == 0)
            {
                return Ok(new { status = "504" });
            }
            if (suesscc.Length > 0)
            {
                var ports = suesscc.ToString().Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var port in ports)
                {
                    var trimmedPort = port.Trim();

                    if (int.TryParse(trimmedPort, out int result))
                    {
                        intResults.Add(result);
                    }
                    else
                    {
                        nonIntResults.Add(trimmedPort);
                    }
                }
                return Ok(new { status = "200", info = intResults.Distinct(), datetime = DateTime.Now.ToString() });
            }
            else
            {
                foreach (var errors in error.ToString())
                {
                    nonIntResults.Add(errors.ToString());
                }
            }

            return Ok(new { status = "500", error = nonIntResults, datetime = DateTime.Now.ToString() });
        }
    }
}
